package com.spikything.gfx {
	
	import com.spikything.utils.AssetLoader;
	import com.spikything.utils.events.AssetLoaderEvent;
	import com.spikything.gfx.RoundFrame;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Rectangle;

	public class PictureFrame extends Sprite {
		
		public var content 		:Sprite;
		
		private var maskSprite 	:RoundFrame;
		private var loader 		:AssetLoader;
		
		public function PictureFrame (url:String, px:Number = 0, py:Number = 0, width:Number = 0, height:Number = 0, bgcolor:Number = 0, bgalpha:Number = 1, round:Number = 0) {
			x = px;
			y = py;
			loader = new AssetLoader();
			loader.addEventListener(AssetLoaderEvent.LOADED, assetLoaded);
			try {
				loader.loadAsset(url, this, 0, 0);
			} catch (err:Error) {
				// fail silently
				trace("ERROR LOADING PICTURE FROM: " + url);
			}
			maskSprite = new RoundFrame(width, height, 0, bgalpha, round);
			addChild(maskSprite);
			loader.scrollRect = new Rectangle(0, 0, width, height);
			mask = maskSprite;
		}
		
		public function kill ():void {
			loader.kill();
			removeChild(loader);
		}
		
		private function assetLoaded(e:AssetLoaderEvent):void {
			// centre in mask
			loader.x = Math.round((loader.width - mask.width) / 2);
			loader.y = Math.round((loader.height - mask.height) / 2);
		}
	
	}

}